package cn.itsource.pet.service.impl;

import cn.itsource.basic.domain.Point;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pet.domain.dto.AcceptSearchMasterMsgDto;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhm
 * @since 2023-04-04
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    @Transactional
    public AjaxResult publish(SearchMasterMsg msg, HttpServletRequest request) {
        //先保存数据
        super.add(msg);
        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        //再审核
        if(BaiduAuditUtils.TextCensor(msg.getName()) && BaiduAuditUtils.TextCensor(msg.getTitle())){
            //审核通过
            log.setState(1);
            log.setNote("审核通过!!!");
            msg.setState(1);
            //匹配最近的店铺
            //查询出所有店铺
            List<Shop> shops = shopMapper.queryAll();
            //算出用户填写地址的左边
            Point point = DistanceUtil.getPoint(msg.getAddress());
            //求最近的店铺
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            if(nearestShop != null){
                msg.setShopId(nearestShop.getId());
            }
        }else{
            log.setState(-1);
            log.setNote("宠物姓名或标题异常!!!");
            //审核不通过
            //修改状态
            msg.setState(-1);
        }
        log.setMsgId(msg.getId());
        log.setAuditTime(new Date());
        //保存审核日志
        searchMasterMsgAuditLogMapper.add(log);
        //更新寻主消息
        super.update(msg);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult reject(Long id) {
        //把消息归还到寻主池
        SearchMasterMsg searchMasterMsg = super.queryById(id);
        searchMasterMsg.setShopId(null);
        super.update(searchMasterMsg);
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult accept(AcceptSearchMasterMsgDto dto) {
        Long msgId = dto.getMsgId();
        Long handler = dto.getHandler();
        String note = dto.getNote();

        //查询寻主消息
        SearchMasterMsg msg = super.queryById(msgId);
        msg.setState(3);
        //创建宠物
        Pet pet = initPet(msgId, msg);
        // 创建收购单
        innitOrderPetAcquisition(msgId, handler, msg, pet);
        //更新寻主消息状态
        super.update(msg);
        return AjaxResult.me();
    }

    private void innitOrderPetAcquisition(Long msgId, Long handler, SearchMasterMsg msg, Pet pet) {
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msgId));
        order.setState(0);
        order.setDigest(msg.getName()+"收狗顶单");
        order.setShopId(msg.getShopId());
        order.setUserId(msg.getUserId());
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+3*24*60*60*1000));
        order.setPetId(pet.getId());
        order.setPrice(msg.getPrice());
        order.setEmpId(handler);
        order.setSearchMasterMsgId(msgId);
        order.setAddress(msg.getAddress());
        orderPetAcquisitionMapper.add(order);
    }

    private Pet initPet(Long msgId, SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setTypeId(msg.getPetType());
        pet.setName(msg.getName());
        pet.setSearchMasterMsgId(msgId);
        pet.setState(0L);
        pet.setCreatetime(new Date());
        pet.setShopId(msg.getShopId());
        petMapper.add(pet);
        return pet;
    }
}
